﻿<script language="JavaScript" type="text/javascript" src="../jquery-1.7.min.js"></script>
<script type="text/javascript" src="../onclick.js"></script>
<?php 
include('../Connections/connect.php'); ?>
<?php
$str = ""; $str_type = ""; $str_subType = ""; $str_data = "";
$type = $_POST['branch'];
$subType = $_POST['zone'];
$data = $_POST['DMA'];
// เชคค่าจาก dropdown 
if($type==0){
	$str_type = "";
	$str_subType = "";
	$str_data = "";
}else{
	if($type!='null' && $subType=='null' && $data=='null'){
		$str_type = " where Branch ='".$type."'";
	}else if($type!='null' && $subType=='' && $data=='null'){
		$str_type = " where Branch ='".$type."'";
	}else if($type!='null' && $subType!='null' && $data==''){
		$str_type = " where Branch ='".$type."'";
		if($subType!='null' && $subType!=''){
			$str_subType = " and zone='".$subType."'";
		}
	}else{
		$str_type = " where Branch ='".$type."'";
		$str_subType = " and zone='".$subType."'";
		$str_data = " and Block='".$data."'";
	}
}	
$str="";
$sqlcommand="select distinct(MATL) as type_name , sum(Shape_Length) as count_amt from pipedata $str_type $str_subType $str_data group by type_name"; // Query ที่ต้องเปลี่ยน เค้าไม่รู้ว่าดึงจาก DB ไหน ดึงมาเป็นตัวอย่างเพื่อจัดรูปแบบ string ให้ตรงกับที่พี่ใช้ในข้อมูลให้ Graph (แก้แค่ SQL กับฟิล์ดที่เลือกมาแสดงพอ)
$result = mysql_query($sqlcommand, $connect)or die(mysql_error()); 
$num = mysql_num_rows($result);
sleep(1);
$i=1;
while ($row = mysql_fetch_assoc($result)) {    
    $firstname = trim(addslashes($row['type_name']));
	$height = $row['count_amt'];
	$sumsrt = changetokilo($height);
	$str .= "['".$firstname."', ".$sumsrt."]";
	if($num-$i>0){
		$str .= ",";
	}
	$i++;
}
 $output=$str;
// check null output
 if($output==null){
	$output = "['', 0]";
	$check = "0";
 }

function changetokilo($height){
    
	$strtest=$height;
	if(strlen($strtest)<3){
		if(strlen($strtest)==3){
			$test = "." .substr($strtest,-3);
		}else if(strlen($strtest)==2){
			$test = ".0" .substr($strtest,-2);
		}else if(strlen($strtest)==1){
			$test = ".00" .substr($strtest,-1);
		}
		
	}else{
		$test = "." .substr($strtest,-3);
	}
	if(strlen($strtest)<3){
		$test2 = "0";
	}else{
		$test2 = substr($strtest,0,-3);
	}
		
	$sumsrt = $test2 . $test ;
	return $sumsrt;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>WMA Grpah</title>
	<script language="JavaScript" type="text/javascript" src="../jquery-1.7.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
	
	<script type="text/javascript">
    var defaultChart=0;
	var graph=0;
	var chart;
	var checknull="0";
	var data =[];
	google.load("visualization", "1", {packages:["corechart"]});
    
	
      	function drawChart(){
			
			checknulldata();
			
			data[100] = google.visualization.arrayToDataTable([
          		['Type', 'ท่อจ่ายน้ำ'],
            		['', 0],['AC', 0],['CI', 0],['DI', 0],['GI', 0],['HDPE', 0],['PB', 0],['PC', 0],['PE', 0],['PVC', 0],['SCP', 0],['ST', 0],['TT', 0]
        	]);
			data[1] = google.visualization.arrayToDataTable([
				['Type', 'ท่อจ่ายน่้ำ'],
					<?php echo $output ?>
				]);
			
			var formatter = new google.visualization.NumberFormat({fractionDigits: -2,prefix: 'ความยาว',suffix: ' กิโลเมตร'});
  			formatter.format(data[1], 1); // Apply formatter to second column
			
			var options = {
				title: ''+checknull,
				animation:{ duration: 1000, easing: 'out'},
				vAxis: {title: 'วัสดุ',  titleTextStyle: {color: 'red'}},
		  		hAxis: {title: "ความยาว (กิโลเมตร)"},
				is3D: true,
			};
			 
			//default BarChart at initial value
			if(defaultChart==0){
				chart = new google.visualization.BarChart(document.getElementById('chart_div'));
				defaultChart=1;
			}
			//var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
			//var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
			document.getElementById('chart_div').style.display = 'block';
			chart.draw(data[100], options);
			chart.draw(data[1], options);
			
     	}
	function changeGraph(){	
		 
		 if(graph==0){
			chart = new google.visualization.PieChart(document.getElementById('chart_div'));
		}
		else{
			chart = new google.visualization.BarChart(document.getElementById('chart_div'));
		}
		 	graph = 1 - graph;
			drawChart();
	}
	function checknulldata(){
		checknull =  "<?php echo $check ?>";
		
		if (checknull=="0"){
			checknull="ขออภัยไม่พบข้อมูลที่ท่านต้องการในเขตพื้นที่นี้";
		}
	
	}
</script>
</head>
<body onload='drawChart()'>
<h2>กราฟแสดงความยาวท่อจ่ายน้ำแยกตามวัสดุ</h2>
<?php
	if($type==0){
		echo "<h2>สำนักงานประปาสาขาทั้งหมด</h2>";
	}else{
		if($type!='null' && $subType=='null' && $data=='null'){
			$str_type = "branch_id ='".$type."'";
		}else if($type!='null' && $subType=='' && $data=='null'){
			$str_type = "branch_id ='".$type."'";
		}else if($type!='null' && $subType!='null' && $data==''){
			$str_type = "branch_id ='".$type."'";
			if($subType!='null' && $subType!=''){
				$str_subType = " and zone_id='".$subType."'";
			}
		}else{
			$str_type = "branch_id ='".$type."'";
			$str_subType = " and zone_id='".$subType."'";
			$str_data = " and DMA_id='".$data."'";
		}
		$str="";
		$sqlcommand="select * from mwa_area where $str_type $str_subType $str_data group by branch_id, zone_id";
		$result = mysql_query($sqlcommand, $connect)or die(mysql_error()); 
		$num = mysql_num_rows($result);
		$row = mysql_fetch_assoc($result);
		echo "<h2>";
		if($type!='null' && $subType=='null' && $data=='null'){
			echo "สำนักงานประปา";
			echo $row['branch_name']." ";
		}else if($type!='null' && $subType=='' && $data=='null'){
			echo "สำนักงานประปา";
			echo $row['branch_name']." ";
		}else if($type!='null' && $subType!='null' && $data==''){
			echo "สำนักงานประปา";
			echo $row['branch_name']." ";
			if($subType!='null' && $subType!=''){
				echo $row['zone_name']." ";
			}
		}else{
			echo "สำนักงานประปา";
			echo $row['branch_name']." ".$row['zone_name']." ".$row['DMA_name'];
		}
		echo "</h2>";
	}
?>

<form>
	<input type="button" value="เปลี่ยนกราฟ" onclick="changeGraph()" /> 
    <input name="btn_back" type="button" id="btn_back" value="Back" onclick="window.location='dropDownList.php'"/>
	
</form>
	<div id="chart_div" style="width: 650px; height: 400px;"></div>
</body>
</html>